<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>roster_user_tools.cli_common_lib &mdash; Roster User Tools v0.18 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     'v0.18',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="Roster User Tools v0.18 documentation" href="../../index.html" />
    <link rel="up" title="Module code" href="../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">Roster User Tools v0.18 documentation</a> &raquo;</li>
          <li><a href="../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for roster_user_tools.cli_common_lib</h1><div class="highlight"><pre>
<span class="c">#!/usr/bin/python</span>

<span class="c"># Copyright (c) 2009, Purdue University</span>
<span class="c"># All rights reserved.</span>
<span class="c">#</span>
<span class="c"># Redistribution and use in source and binary forms, with or without</span>
<span class="c"># modification, are permitted provided that the following conditions are met:</span>
<span class="c">#</span>
<span class="c"># Redistributions of source code must retain the above copyright notice, this</span>
<span class="c"># list of conditions and the following disclaimer.</span>
<span class="c">#</span>
<span class="c"># Redistributions in binary form must reproduce the above copyright notice, this</span>
<span class="c"># list of conditions and the following disclaimer in the documentation and/or</span>
<span class="c"># other materials provided with the distribution.</span>
<span class="c">#</span>
<span class="c"># Neither the name of the Purdue University nor the names of its contributors</span>
<span class="c"># may be used to endorse or promote products derived from this software without</span>
<span class="c"># specific prior written permission.</span>
<span class="c">#</span>
<span class="c"># THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot;</span>
<span class="c"># AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span>
<span class="c"># IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE</span>
<span class="c"># DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE</span>
<span class="c"># FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL</span>
<span class="c"># DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR</span>
<span class="c"># SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER</span>
<span class="c"># CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,</span>
<span class="c"># OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE</span>
<span class="c"># OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span>

<span class="sd">&quot;&quot;&quot;Common library for DNS management command line tools.&quot;&quot;&quot;</span>

<span class="n">__copyright__</span> <span class="o">=</span> <span class="s">&#39;Copyright (C) 2009, Purdue University&#39;</span>
<span class="n">__license__</span> <span class="o">=</span> <span class="s">&#39;BSD&#39;</span>
<span class="n">__version__</span> <span class="o">=</span> <span class="s">&#39;#TRUNK#&#39;</span>


<span class="kn">import</span> <span class="nn">ConfigParser</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">roster_client_lib</span>

<div class="viewcode-block" id="ArgumentError"><a class="viewcode-back" href="../../roster_user_tools.html#roster_user_tools.cli_common_lib.ArgumentError">[docs]</a><span class="k">class</span> <span class="nc">ArgumentError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
  <span class="k">pass</span>
</div>
<div class="viewcode-block" id="HostsError"><a class="viewcode-back" href="../../roster_user_tools.html#roster_user_tools.cli_common_lib.HostsError">[docs]</a><span class="k">class</span> <span class="nc">HostsError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
  <span class="k">pass</span>

</div>
<span class="n">DEFAULT_CRED_FILE</span> <span class="o">=</span> <span class="s">&#39;~/.dnscred&#39;</span>
<span class="n">DEFAULT_USER_CONFIG_FILE</span> <span class="o">=</span> <span class="s">&#39;/etc/roster/roster_user_tools.conf&#39;</span>
<span class="n">DEFAULT_RC_FILE</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">expanduser</span><span class="p">(</span><span class="s">&#39;~/.rosterrc&#39;</span><span class="p">)</span>


<div class="viewcode-block" id="CliCommonLib"><a class="viewcode-back" href="../../roster_user_tools.html#roster_user_tools.cli_common_lib.CliCommonLib">[docs]</a><span class="k">class</span> <span class="nc">CliCommonLib</span><span class="p">:</span>
  <span class="sd">&quot;&quot;&quot;Command line common library&quot;&quot;&quot;</span>
  <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">options</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Retrieves configuration</span>

<span class="sd">    Inputs:</span>
<span class="sd">      options: Options object from optparse</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">options</span> <span class="o">=</span> <span class="n">options</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">config_file</span> <span class="o">=</span> <span class="n">ConfigParser</span><span class="o">.</span><span class="n">SafeConfigParser</span><span class="p">()</span>
    <span class="k">if</span><span class="p">(</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="s">&#39;config_file&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">options</span><span class="o">.</span><span class="n">config_file</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="p">):</span>
      <span class="n">config_file</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="o">.</span><span class="n">config_file</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">config_file</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">config_file</span><span class="p">)</span>
      <span class="k">if</span><span class="p">(</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">,</span> <span class="s">&#39;server&#39;</span><span class="p">)</span> <span class="p">):</span>
        <span class="k">if</span><span class="p">(</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="o">.</span><span class="n">server</span> <span class="p">):</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="o">.</span><span class="n">server</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">config_file</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;user_tools&#39;</span><span class="p">,</span> <span class="s">&#39;server&#39;</span><span class="p">)</span>
      <span class="k">if</span><span class="p">(</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">,</span> <span class="s">&#39;credfile&#39;</span><span class="p">)</span> <span class="p">):</span>
        <span class="k">if</span><span class="p">(</span> <span class="ow">not</span> <span class="n">options</span><span class="o">.</span><span class="n">credfile</span> <span class="p">):</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="o">.</span><span class="n">credfile</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">config_file</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;user_tools&#39;</span><span class="p">,</span>
                                                       <span class="s">&#39;cred_file&#39;</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
      <span class="n">config_file</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
      <span class="k">if</span><span class="p">(</span> <span class="s">&#39;ROSTER_USER_CONFIG&#39;</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span> <span class="p">):</span>
        <span class="n">file_locations</span> <span class="o">=</span> <span class="p">[</span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s">&#39;ROSTER_USER_CONFIG&#39;</span><span class="p">]]</span>
      <span class="k">else</span><span class="p">:</span>
        <span class="n">file_locations</span> <span class="o">=</span> <span class="p">[]</span>
      <span class="n">file_locations</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">DEFAULT_RC_FILE</span><span class="p">,</span> <span class="n">DEFAULT_USER_CONFIG_FILE</span><span class="p">])</span>
      <span class="k">for</span> <span class="n">config_file</span> <span class="ow">in</span> <span class="n">file_locations</span><span class="p">:</span>
        <span class="k">if</span><span class="p">(</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">config_file</span><span class="p">)</span> <span class="p">):</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">config_file</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">config_file</span><span class="p">)</span>
          <span class="k">if</span><span class="p">(</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">,</span> <span class="s">&#39;server&#39;</span><span class="p">)</span> <span class="p">):</span>
            <span class="k">if</span><span class="p">(</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="o">.</span><span class="n">server</span> <span class="p">):</span>
              <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="o">.</span><span class="n">server</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">config_file</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;user_tools&#39;</span><span class="p">,</span> <span class="s">&#39;server&#39;</span><span class="p">)</span>
          <span class="k">if</span><span class="p">(</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">,</span> <span class="s">&#39;credfile&#39;</span><span class="p">)</span> <span class="p">):</span>
            <span class="k">if</span><span class="p">(</span> <span class="ow">not</span> <span class="n">options</span><span class="o">.</span><span class="n">credfile</span> <span class="p">):</span>
              <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="o">.</span><span class="n">credfile</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">config_file</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;user_tools&#39;</span><span class="p">,</span>
                                                           <span class="s">&#39;cred_file&#39;</span><span class="p">)</span>
          <span class="k">break</span>
      <span class="k">else</span><span class="p">:</span>
        <span class="k">if</span><span class="p">(</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">,</span> <span class="s">&#39;server&#39;</span><span class="p">)</span> <span class="p">):</span>
          <span class="k">if</span><span class="p">(</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="o">.</span><span class="n">server</span> <span class="p">):</span>
            <span class="k">raise</span> <span class="n">ArgumentError</span><span class="p">(</span><span class="s">&#39;A server must be specified.&#39;</span><span class="p">)</span>
        <span class="k">if</span><span class="p">(</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">,</span> <span class="s">&#39;credfile&#39;</span><span class="p">)</span> <span class="p">):</span>
          <span class="k">if</span><span class="p">(</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="o">.</span><span class="n">credfile</span> <span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="o">.</span><span class="n">credfile</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">expanduser</span><span class="p">(</span><span class="n">DEFAULT_CRED_FILE</span><span class="p">)</span>

    <span class="n">roster_client_lib</span><span class="o">.</span><span class="n">CheckServerVersionMatch</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="o">.</span><span class="n">server</span><span class="p">)</span>
    <span class="n">roster_client_lib</span><span class="o">.</span><span class="n">CheckCredentials</span><span class="p">(</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="o">.</span><span class="n">username</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="o">.</span><span class="n">credfile</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="o">.</span><span class="n">server</span><span class="p">,</span>
        <span class="n">password</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="o">.</span><span class="n">password</span><span class="p">)</span>

<div class="viewcode-block" id="CliCommonLib.DisallowFlags"><a class="viewcode-back" href="../../roster_user_tools.html#roster_user_tools.cli_common_lib.CliCommonLib.DisallowFlags">[docs]</a>  <span class="k">def</span> <span class="nf">DisallowFlags</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">disallow_list</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Dissallows certain command line flags.</span>

<span class="sd">    Inputs:</span>
<span class="sd">      disallow_list: list of command line flags to block</span>
<span class="sd">      parser: parser object from optparse</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">defaults</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">defaults</span>
    <span class="n">error</span> <span class="o">=</span> <span class="bp">False</span>
    <span class="k">for</span> <span class="n">flag</span> <span class="ow">in</span> <span class="n">parser</span><span class="o">.</span><span class="n">option_list</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span>
      <span class="n">combo</span> <span class="o">=</span> <span class="s">&#39;self.options.</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">flag</span><span class="o">.</span><span class="n">dest</span>
      <span class="k">if</span><span class="p">(</span> <span class="n">flag</span><span class="o">.</span><span class="n">dest</span> <span class="ow">in</span> <span class="n">disallow_list</span> <span class="p">):</span>
        <span class="k">if</span><span class="p">(</span> <span class="nb">eval</span><span class="p">(</span><span class="n">combo</span><span class="p">)</span> <span class="o">!=</span> <span class="n">defaults</span><span class="p">[</span><span class="n">flag</span><span class="o">.</span><span class="n">dest</span><span class="p">]</span> <span class="p">):</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">DnsError</span><span class="p">(</span><span class="s">&#39;The </span><span class="si">%s</span><span class="s"> flag cannot be used.&#39;</span> <span class="o">%</span> <span class="n">flag</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
          <span class="n">error</span> <span class="o">=</span> <span class="bp">True</span>
    <span class="k">if</span><span class="p">(</span> <span class="n">error</span> <span class="p">):</span>
      <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

  <span class="c">## Function accessors, need to be removed at some point</span></div></div>
<div class="viewcode-block" id="SortRecordsDict"><a class="viewcode-back" href="../../roster_user_tools.html#roster_user_tools.cli_common_lib.SortRecordsDict">[docs]</a><span class="k">def</span> <span class="nf">SortRecordsDict</span><span class="p">(</span><span class="n">records_dictionary</span><span class="p">,</span> <span class="n">view_name</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot;Retries records from database and sorts them</span>

<span class="sd">  Inputs:</span>
<span class="sd">    records_dictionary: dictionary of records from core</span>
<span class="sd">    view_name: string of view name</span>

<span class="sd">  Outputs:</span>
<span class="sd">    dict: sorted dictionary of records</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="n">sorted_records</span> <span class="o">=</span> <span class="p">{}</span>
  <span class="k">for</span> <span class="n">ip_address</span> <span class="ow">in</span> <span class="n">records_dictionary</span><span class="p">[</span><span class="n">view_name</span><span class="p">]:</span>
    <span class="k">for</span> <span class="n">record</span> <span class="ow">in</span> <span class="n">records_dictionary</span><span class="p">[</span><span class="n">view_name</span><span class="p">][</span><span class="n">ip_address</span><span class="p">]:</span>
      <span class="n">record</span><span class="p">[</span><span class="s">&#39;view&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">view_name</span>
      <span class="k">if</span><span class="p">(</span> <span class="n">ip_address</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">sorted_records</span> <span class="p">):</span>
        <span class="n">sorted_records</span><span class="p">[</span><span class="n">ip_address</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;forward&#39;</span><span class="p">:</span> <span class="p">[],</span> <span class="s">&#39;reverse&#39;</span><span class="p">:</span> <span class="p">[]}</span>
      <span class="k">if</span><span class="p">(</span> <span class="n">record</span><span class="p">[</span><span class="s">&#39;forward&#39;</span><span class="p">]</span> <span class="p">):</span>
        <span class="n">sorted_records</span><span class="p">[</span><span class="n">ip_address</span><span class="p">][</span><span class="s">&#39;forward&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">record</span><span class="p">)</span>
      <span class="k">else</span><span class="p">:</span>
        <span class="n">sorted_records</span><span class="p">[</span><span class="n">ip_address</span><span class="p">][</span><span class="s">&#39;reverse&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">record</span><span class="p">)</span>
  <span class="k">return</span> <span class="n">sorted_records</span>
</div>
<div class="viewcode-block" id="DnsError"><a class="viewcode-back" href="../../roster_user_tools.html#roster_user_tools.cli_common_lib.DnsError">[docs]</a><span class="k">def</span> <span class="nf">DnsError</span><span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="n">exit_status</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot;Prints standardized client error message to screen.</span>

<span class="sd">  Inputs:</span>
<span class="sd">    message: string of message to be displayed on screen</span>
<span class="sd">    exit_status: integer of retrun code, assumed not exit if 0</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="k">print</span> <span class="s">&quot;CLIENT ERROR: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">message</span>
  <span class="k">if</span><span class="p">(</span> <span class="n">exit_status</span> <span class="p">):</span>
    <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="n">exit_status</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="ServerError"><a class="viewcode-back" href="../../roster_user_tools.html#roster_user_tools.cli_common_lib.ServerError">[docs]</a><span class="k">def</span> <span class="nf">ServerError</span><span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="n">uuid_string</span><span class="p">,</span> <span class="n">exit_status</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot;Prints standardized server error message to screen.</span>

<span class="sd">  Inputs:</span>
<span class="sd">    message: string of message to be displayed on screen</span>
<span class="sd">    exit_status: integer of retrun code, assumed not exit if 0</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="k">print</span> <span class="s">&quot;SERVER ERROR: (</span><span class="si">%s</span><span class="s">) </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">uuid_string</span><span class="p">,</span> <span class="n">message</span><span class="p">)</span>
  <span class="k">if</span><span class="p">(</span> <span class="n">exit_status</span> <span class="p">):</span>
    <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="n">exit_status</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="UserError"><a class="viewcode-back" href="../../roster_user_tools.html#roster_user_tools.cli_common_lib.UserError">[docs]</a><span class="k">def</span> <span class="nf">UserError</span><span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="n">exit_status</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot;Prints standardized user error message to screen.</span>

<span class="sd">  Inputs:</span>
<span class="sd">    message: string of message to be displayed on screen</span>
<span class="sd">    exit_status: integer of retrun code, assumed not exit if 0</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="k">print</span> <span class="s">&quot;USER ERROR: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">message</span><span class="p">)</span>
  <span class="k">if</span><span class="p">(</span> <span class="n">exit_status</span> <span class="p">):</span>
    <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="n">exit_status</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="UnknownError"><a class="viewcode-back" href="../../roster_user_tools.html#roster_user_tools.cli_common_lib.UnknownError">[docs]</a><span class="k">def</span> <span class="nf">UnknownError</span><span class="p">(</span><span class="n">error_class</span><span class="p">,</span> <span class="n">uuid_string</span><span class="p">,</span> <span class="n">message</span><span class="p">,</span> <span class="n">exit_status</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot;Prints standardized unknown error message to screen.</span>

<span class="sd">  Inputs:</span>
<span class="sd">    message: string of message to be displayed on screen</span>
<span class="sd">    exit_status: integer of retrun code, assumed not exit if 0</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="k">print</span> <span class="s">&quot;UNKNOWN ERROR(</span><span class="si">%s</span><span class="s">): (</span><span class="si">%s</span><span class="s">) </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">error_class</span><span class="p">,</span> <span class="n">uuid_string</span><span class="p">,</span>
                                        <span class="n">message</span><span class="p">)</span>
  <span class="k">if</span><span class="p">(</span> <span class="n">exit_status</span> <span class="p">):</span>
    <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="n">exit_status</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="DnsWarning"><a class="viewcode-back" href="../../roster_user_tools.html#roster_user_tools.cli_common_lib.DnsWarning">[docs]</a><span class="k">def</span> <span class="nf">DnsWarning</span><span class="p">(</span><span class="n">message</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot;Prints standardized warning message to screen.</span>

<span class="sd">  Inputs:</span>
<span class="sd">    message: string of message to be displayed on screen</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="k">print</span> <span class="s">&quot;WARNING: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">message</span>
</div>
<div class="viewcode-block" id="PrintColumns"><a class="viewcode-back" href="../../roster_user_tools.html#roster_user_tools.cli_common_lib.PrintColumns">[docs]</a><span class="k">def</span> <span class="nf">PrintColumns</span><span class="p">(</span><span class="n">print_list</span><span class="p">,</span> <span class="n">first_line_header</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot;Prints a table with aligned columns.</span>

<span class="sd">  Inputs:</span>
<span class="sd">    print_list: list of lists of columns</span>
<span class="sd">    file: string of filename</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="c">## Construct zeros for lengths</span>
  <span class="n">lengths</span> <span class="o">=</span> <span class="p">[]</span>
  <span class="k">if</span><span class="p">(</span> <span class="n">print_list</span> <span class="p">):</span>
    <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">print_list</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span>
      <span class="n">lengths</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
  <span class="c">## Get sizes of strings</span>
  <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">print_list</span><span class="p">:</span>
    <span class="k">for</span> <span class="n">column_index</span><span class="p">,</span> <span class="n">column</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">row</span><span class="p">):</span>
      <span class="k">if</span><span class="p">(</span> <span class="nb">len</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">column</span><span class="p">))</span> <span class="o">&gt;</span> <span class="n">lengths</span><span class="p">[</span><span class="n">column_index</span><span class="p">]</span> <span class="p">):</span>
        <span class="n">lengths</span><span class="p">[</span><span class="n">column_index</span><span class="p">]</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">column</span><span class="p">))</span>
  <span class="c">## Construct string</span>
  <span class="n">print_string_list</span> <span class="o">=</span> <span class="p">[]</span>
  <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">print_list</span><span class="p">:</span>
    <span class="n">string_list</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">string_args_list</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">column_index</span><span class="p">,</span> <span class="n">column</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">print_list</span><span class="p">[</span><span class="mi">0</span><span class="p">]):</span>
      <span class="n">string_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%*s</span><span class="s">&#39;</span><span class="p">)</span>
      <span class="n">string_args_list</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">lengths</span><span class="p">[</span><span class="n">column_index</span><span class="p">]</span> <span class="o">*</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">row</span><span class="p">[</span><span class="n">column_index</span><span class="p">]])</span>
    <span class="n">print_line</span> <span class="o">=</span> <span class="s">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">string_list</span><span class="p">)</span> <span class="o">%</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">string_args_list</span><span class="p">)</span>
    <span class="n">print_string_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">print_line</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span>
    <span class="k">if</span><span class="p">(</span> <span class="n">first_line_header</span> <span class="ow">and</span> <span class="p">(</span><span class="n">print_list</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">row</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">):</span>
      <span class="n">hyphen_list</span> <span class="o">=</span> <span class="p">[]</span>
      <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">print_line</span><span class="o">.</span><span class="n">strip</span><span class="p">())):</span>
        <span class="n">hyphen_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#39;-&#39;</span><span class="p">)</span>
      <span class="n">print_string_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="s">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">hyphen_list</span><span class="p">))</span>
  <span class="k">return</span> <span class="s">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">print_string_list</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="PrintSortedHosts"><a class="viewcode-back" href="../../roster_user_tools.html#roster_user_tools.cli_common_lib.PrintSortedHosts">[docs]</a><span class="k">def</span> <span class="nf">PrintSortedHosts</span><span class="p">(</span><span class="n">hosts_dict</span><span class="p">,</span> <span class="n">print_headers</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot;Prints sorted hosts in a nice usable format</span>

<span class="sd">  Inputs:</span>
<span class="sd">    hosts_dict: A dictionary of sort host lists keyed by views.</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="n">print_list</span> <span class="o">=</span> <span class="p">[]</span>
  <span class="k">for</span> <span class="n">view</span> <span class="ow">in</span> <span class="n">hosts_dict</span><span class="p">:</span>
    <span class="k">if</span><span class="p">(</span> <span class="n">print_headers</span> <span class="p">):</span>
      <span class="n">print_list</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="s">&#39;View:&#39;</span><span class="p">,</span> <span class="n">view</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">])</span>
    <span class="k">for</span> <span class="n">host</span> <span class="ow">in</span> <span class="n">hosts_dict</span><span class="p">[</span><span class="n">view</span><span class="p">]:</span>
      <span class="n">print_list</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">host</span><span class="p">[</span><span class="s">&#39;ip_address&#39;</span><span class="p">],</span> <span class="n">host</span><span class="p">[</span><span class="s">&#39;direction&#39;</span><span class="p">],</span> <span class="n">host</span><span class="p">[</span><span class="s">&#39;host&#39;</span><span class="p">],</span>
                         <span class="n">host</span><span class="p">[</span><span class="s">&#39;record_zone_name&#39;</span><span class="p">],</span> <span class="n">view</span><span class="p">])</span>
  <span class="k">return</span> <span class="n">PrintColumns</span><span class="p">(</span><span class="n">print_list</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="PrintHosts"><a class="viewcode-back" href="../../roster_user_tools.html#roster_user_tools.cli_common_lib.PrintHosts">[docs]</a><span class="k">def</span> <span class="nf">PrintHosts</span><span class="p">(</span><span class="n">records_dictionary</span><span class="p">,</span> <span class="n">ip_address_list</span><span class="p">,</span> <span class="n">view_name</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot;Prints hosts in an /etc/hosts format</span>

<span class="sd">  Inputs:</span>
<span class="sd">    records_dictionary: dictionary of records</span>
<span class="sd">    ip_address_list: list of ip_addresses</span>
<span class="sd">    view_name: string of view_name</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="n">print_list</span> <span class="o">=</span> <span class="p">[]</span>
  <span class="n">sorted_records</span> <span class="o">=</span> <span class="n">SortRecordsDict</span><span class="p">(</span><span class="n">records_dictionary</span><span class="p">,</span> <span class="n">view_name</span><span class="p">)</span>
  <span class="k">for</span> <span class="n">ip_address</span> <span class="ow">in</span> <span class="n">ip_address_list</span><span class="p">:</span>
    <span class="k">if</span><span class="p">(</span> <span class="n">ip_address</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">sorted_records</span> <span class="p">):</span>
      <span class="n">print_list</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="s">&#39;#</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">ip_address</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">])</span>
      <span class="k">continue</span>
    <span class="k">if</span><span class="p">(</span> <span class="nb">len</span><span class="p">(</span><span class="n">sorted_records</span><span class="p">[</span><span class="n">ip_address</span><span class="p">][</span><span class="s">&#39;reverse&#39;</span><span class="p">])</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="p">):</span>
      <span class="k">raise</span> <span class="n">HostsError</span><span class="p">(</span>
          <span class="s">&#39;Multiple reverse records found for </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span>
              <span class="nb">str</span><span class="p">(</span><span class="n">sorted_records</span><span class="p">[</span><span class="n">ip_address</span><span class="p">])))</span>
    <span class="k">elif</span><span class="p">(</span> <span class="nb">len</span><span class="p">(</span><span class="n">sorted_records</span><span class="p">[</span><span class="n">ip_address</span><span class="p">][</span><span class="s">&#39;reverse&#39;</span><span class="p">])</span> <span class="o">==</span> <span class="mi">0</span> <span class="p">):</span>
      <span class="k">for</span> <span class="n">record</span> <span class="ow">in</span> <span class="n">sorted_records</span><span class="p">[</span><span class="n">ip_address</span><span class="p">][</span><span class="s">&#39;forward&#39;</span><span class="p">]:</span>
        <span class="n">forward_zone_origin</span> <span class="o">=</span> <span class="n">record</span><span class="p">[</span><span class="s">&#39;zone_origin&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s">&#39;.&#39;</span><span class="p">)</span>
        <span class="n">shorthost</span> <span class="o">=</span> <span class="n">record</span><span class="p">[</span><span class="s">&#39;host&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">rsplit</span><span class="p">(</span><span class="s">&#39;.</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">forward_zone_origin</span><span class="p">,</span> <span class="mi">1</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">longhost</span> <span class="o">=</span> <span class="n">record</span><span class="p">[</span><span class="s">&#39;host&#39;</span><span class="p">]</span>
        <span class="k">if</span><span class="p">(</span> <span class="n">longhost</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;@.&#39;</span><span class="p">)</span> <span class="p">):</span>
          <span class="n">longhost</span> <span class="o">=</span> <span class="n">record</span><span class="p">[</span><span class="s">&#39;host&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lstrip</span><span class="p">(</span><span class="s">&#39;@.&#39;</span><span class="p">)</span>
        <span class="n">print_list</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="s">&#39;#</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">ip_address</span><span class="p">,</span> <span class="n">longhost</span><span class="p">,</span>
                           <span class="n">shorthost</span><span class="p">,</span> <span class="s">&#39;# No reverse assignment&#39;</span><span class="p">])</span>
    <span class="k">else</span><span class="p">:</span>
      <span class="n">reverse_record</span> <span class="o">=</span> <span class="n">sorted_records</span><span class="p">[</span><span class="n">ip_address</span><span class="p">][</span><span class="s">&#39;reverse&#39;</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
      <span class="k">for</span> <span class="n">record_number</span><span class="p">,</span> <span class="n">record</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span>
          <span class="n">sorted_records</span><span class="p">[</span><span class="n">ip_address</span><span class="p">][</span><span class="s">&#39;forward&#39;</span><span class="p">]):</span>
        <span class="k">if</span><span class="p">(</span> <span class="n">record</span><span class="p">[</span><span class="s">&#39;host&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;@.&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">)</span> <span class="o">==</span>
            <span class="n">reverse_record</span><span class="p">[</span><span class="s">&#39;host&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;@.&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">)</span> <span class="p">):</span>
          <span class="n">forward_zone_origin</span> <span class="o">=</span> <span class="n">record</span><span class="p">[</span><span class="s">&#39;zone_origin&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s">&#39;.&#39;</span><span class="p">)</span>
          <span class="n">shorthost</span> <span class="o">=</span> <span class="n">record</span><span class="p">[</span><span class="s">&#39;host&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">rsplit</span><span class="p">(</span><span class="s">&#39;.</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">forward_zone_origin</span><span class="p">,</span> <span class="mi">1</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
          <span class="n">longhost</span> <span class="o">=</span> <span class="n">record</span><span class="p">[</span><span class="s">&#39;host&#39;</span><span class="p">]</span>
          <span class="k">if</span><span class="p">(</span> <span class="n">longhost</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;@.&#39;</span><span class="p">)</span> <span class="p">):</span>
            <span class="n">longhost</span> <span class="o">=</span> <span class="n">record</span><span class="p">[</span><span class="s">&#39;host&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lstrip</span><span class="p">(</span><span class="s">&#39;@.&#39;</span><span class="p">)</span>
          <span class="n">print_list</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">ip_address</span><span class="p">,</span> <span class="n">longhost</span><span class="p">,</span> <span class="n">shorthost</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">])</span>
          <span class="n">sorted_records</span><span class="p">[</span><span class="n">ip_address</span><span class="p">][</span><span class="s">&#39;forward&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">record_number</span><span class="p">)</span>
          <span class="k">break</span>
      <span class="k">else</span><span class="p">:</span>
        <span class="n">print_list</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="s">&#39;#</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">ip_address</span><span class="p">,</span> <span class="n">reverse_record</span><span class="p">[</span><span class="s">&#39;host&#39;</span><span class="p">],</span>
                           <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;# No forward assignment&#39;</span><span class="p">])</span>
      <span class="k">for</span> <span class="n">record</span> <span class="ow">in</span> <span class="n">sorted_records</span><span class="p">[</span><span class="n">ip_address</span><span class="p">][</span><span class="s">&#39;forward&#39;</span><span class="p">]:</span>
        <span class="n">forward_zone_origin</span> <span class="o">=</span> <span class="n">record</span><span class="p">[</span><span class="s">&#39;zone_origin&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s">&#39;.&#39;</span><span class="p">)</span>
        <span class="n">shorthost</span> <span class="o">=</span> <span class="n">record</span><span class="p">[</span><span class="s">&#39;host&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">rsplit</span><span class="p">(</span><span class="s">&#39;.</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">forward_zone_origin</span><span class="p">,</span> <span class="mi">1</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">longhost</span> <span class="o">=</span> <span class="n">record</span><span class="p">[</span><span class="s">&#39;host&#39;</span><span class="p">]</span>
        <span class="k">if</span><span class="p">(</span> <span class="n">longhost</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;@.&#39;</span><span class="p">)</span> <span class="p">):</span>
          <span class="n">longhost</span> <span class="o">=</span> <span class="n">record</span><span class="p">[</span><span class="s">&#39;host&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lstrip</span><span class="p">(</span><span class="s">&#39;@.&#39;</span><span class="p">)</span>
        <span class="n">print_list</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="s">&#39;#</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">ip_address</span><span class="p">,</span> <span class="n">longhost</span><span class="p">,</span>
                           <span class="n">shorthost</span><span class="p">,</span> <span class="s">&#39;# No reverse assignment&#39;</span><span class="p">])</span>
  <span class="k">return</span> <span class="n">PrintColumns</span><span class="p">(</span><span class="n">print_list</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="EditFile"><a class="viewcode-back" href="../../roster_user_tools.html#roster_user_tools.cli_common_lib.EditFile">[docs]</a><span class="k">def</span> <span class="nf">EditFile</span><span class="p">(</span><span class="n">fname</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot;Opens a file in a text editor in the EDITOR env variable for editing</span>

<span class="sd">  Inputs:</span>
<span class="sd">    fname: string of filename</span>
<span class="sd">  Outputs:</span>
<span class="sd">    int: return code from editor</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="k">if</span><span class="p">(</span> <span class="s">&#39;EDITOR&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span> <span class="p">):</span>
    <span class="n">DnsError</span><span class="p">(</span><span class="s">&#39;EDITOR environment variable not set.&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
  <span class="n">closenum</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s">&#39;EDITOR&#39;</span><span class="p">],</span> <span class="n">fname</span><span class="p">))</span>

  <span class="k">if</span><span class="p">(</span> <span class="n">closenum</span> <span class="ow">is</span> <span class="bp">None</span> <span class="p">):</span>
    <span class="n">return_code</span> <span class="o">=</span> <span class="mi">0</span>
  <span class="k">else</span><span class="p">:</span>
    <span class="n">return_code</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">WEXITSTATUS</span><span class="p">(</span><span class="n">closenum</span><span class="p">)</span>

  <span class="k">if</span><span class="p">(</span> <span class="n">return_code</span> <span class="o">!=</span> <span class="mi">0</span> <span class="p">):</span>
    <span class="n">DnsError</span><span class="p">(</span><span class="s">&#39;Error editing file.&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>

  <span class="k">return</span> <span class="n">return_code</span></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">Roster User Tools v0.18 documentation</a> &raquo;</li>
          <li><a href="../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2013, Purdue University.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b1.
    </div>
  </body>
</html>